#!/usr/bin/env python
#coding=utf-8
# author:wade
# contact: 317909531@qq.com
# datetime:2020/4/4 21:13

from rest_framework import serializers
from wangming.utils.serializers import BaseSerializer
from wangming import models


'''
树形结构
Type
    --Score1
    --Score1
    --Score1
'''

class Tree_WangMing_Score_Seralizer(serializers.ModelSerializer):
    def to_representation(self, instance):  #对于空值进行处理，防止给前端的是null
        data = super().to_representation(instance)  #遍历来处理过滤
        for i in data:
            if not data[i]:
                data[i] = ""

        wordList = []
        wordList.append(data['word1'])
        wordList.append(data['word2'])
        wordList.append(data['word3'])
        wordList.append(data['word4'])
        wordList.append(data['word5'])
        wordList.append(data['word6'])
        data['word1'] = wordList

        return data

    WordList = BaseSerializer.B_WangMing_CharS_serializers(many=True,read_only=True,source='charscore')
    # children = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    class Meta:
        model = models.WangMing_Score
        fields =['fenxiangid', 'fenxiangname','fenxiangIMG','score_type','word', 'statues', 'itemweight', 'word', 'WordList','templateName','templateImgSrc', 'word1', 'word2', 'word3', 'word4', 'word5', 'word6']
        # fields ='__all__'

class Tree_WangMing_Type_Seralizer(serializers.ModelSerializer):
    children = Tree_WangMing_Score_Seralizer(many=True,read_only=True,source='scoretype')
    # children = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
    class Meta:
        model = models.WangMing_Type
        fields =['typename', 'typeweight', 'children']
        # fields ='__all__'






